var express = require('express');
var router = express.Router();
let db =require('../tools/DB');
let to = require('../tools/to');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.json({code:200,msg:'欢迎访问小u课堂API v1.0',data:null});
});

//渲染轮播图
router.get('/banner',async (req,res)=>{
  let sql = 'select id,concat("/uploads/",image_src) as image_src,title,isshow from e_banners';
  let [err,result] = await to(db.query(sql,[]));
  if(!err){
    res.json({code:200,msg:'轮播图渲染成功',data:result});
  }else{
    res.json({code:200,msg:'轮播图渲染失败',data:err});
  }
});

//渲染同步课堂
router.get('/courselist',async (req,res)=>{
  let {type,num} = req.query;
  let sql = "select id,cid,title,price,concat('/uploads/course/',image_src) as image_src,type_name,area_name,subject_name from e_course where type=? limit 0,"+num;
  let [err,result] = await to(db.query(sql,[type]));
  if(!err){
    res.json({code:200,msg:'课程渲染成功',data:result});
  }else{
    res.json({code:200,msg:'课程渲染失败',data:err});
  }
})





module.exports = router;
